﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.ProductInfo;
using com.subb.ProductInfo;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class ProductCataController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllProductCataByPage(StoreRequestParameters parms)
        {
            PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
            int ttlCount;
            List<PP_ProductCata> ppcataList = pcdal.GetAllProductCata(parms.Start, parms.Limit, out ttlCount);
            List<ProductCata> pcatList = new List<ProductCata>();
            foreach (var ppcata in ppcataList)
            {
                string parentCataName = string.Empty;
                if (ppcata.ParentCataID.HasValue && ppcata.ParentCataID.Value > 0)
                {
                    PP_ProductCata parentCata = pcdal.GetProductCata(ppcata.ParentCataID.Value);
                    if (parentCata != null)
                    {
                        parentCataName = parentCata.ProdCataName;
                    }
                }
                ProductCata pcat = ProductCata.AsProductCata(ppcata, parentCataName);
                pcatList.Add(pcat);
            }
            return this.Store(pcatList, ttlCount);
        }

        public ActionResult GetAllProductCata(int? cataId)
        {
            PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
            List<PP_ProductCata> ppcataList = pcdal.GetAllProductCata();
            if (cataId.HasValue)
            {
                PP_ProductCata ppcata = ppcataList.SingleOrDefault(c => c.ProdCataID == cataId);
                if (ppcata != null)
                {
                    ppcataList.Remove(ppcata);
                }
            }
            List<ProductCata> pcatList = new List<ProductCata>();
            foreach (var ppcata in ppcataList)
            {
                ProductCata pcat = ProductCata.AsProductCata(ppcata, "");
                pcatList.Add(pcat);
            }
            return this.Store(pcatList);
        }

        [HttpPost]
        public ActionResult DeleteProductCata(List<ProductCata> delCatas)
        {
            try
            {
                PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
                foreach (var cata in delCatas)
                {
                    pcdal.RemoveProductCata(cata.ProdCataID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewProdCataWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建目录",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateProductCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateProductCata()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateProductCata(ProductCata prodcata)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                string creator = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                prodcata.Creator = creator;
                PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
                pcdal.CreateProductCata(prodcata);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "目录创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditProdCataWindow(int cataId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑目录",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditProductCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("cataId", cataId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditProductCata(int cataId)
        {
            PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
            PP_ProductCata ppcata = pcdal.GetProductCata(cataId);
            //这里不需要显示ParentCataName，所以赋值为空
            ProductCata pcat = ProductCata.AsProductCata(ppcata, "");
            return View(pcat);
        }

        [HttpPost]
        public ActionResult EditProductCata(ProductCata pcata)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                PP_ProductCataDAL pcdal = new PP_ProductCataDAL();
                pcdal.UpdateProductCata(pcata.ProdCataID, pcata);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "目录更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
